package com.senmiao.ssm.exception;

import com.senmiao.ssm.utils.Result;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 */
@RestControllerAdvice
public class GlobalExceptionHandler{

    @ExceptionHandler({UnauthenticatedException.class})
    public Result doUnauthenticatedException(UnauthenticatedException e){
        e.printStackTrace();
        return Result.error().message("用户未登录");
    }

    @ExceptionHandler({UnknownAccountException.class})
    public Result doUnknownAccountException(){
        return Result.error().message("用户名不存在");
    }

    @ExceptionHandler({IncorrectCredentialsException.class})
    public Result doIncorrectCredentialsException(){
        return Result.error().message("密码错误");
    }

    @ExceptionHandler({UnauthorizedException.class})
    public Result doUnauthorizedException(UnauthorizedException e){
        e.printStackTrace();
        return Result.error().message("权限不足");
    }

    /**
     * 异常处理，处理抛出的自定义的异常
     * @param e
     * @return
     */
    @ExceptionHandler({MyException.class})
    public Result myException(MyException e){
        e.printStackTrace();
        return Result.error().message(e.getMessage());
    }

    /**
     * 异常处理，处理未处理的异常
     * @param e
     * @return
     */
    @ExceptionHandler({Exception.class})
    public Result allException(Exception e){
        e.printStackTrace();
        return Result.serverError();
    }

}